import { Message } from 'element-ui'
import { addOrUpdate } from '@/api/active-item'
import { batchValidate } from '@/api/active-item'

export function showMessage(obj, val) {
  var msgs = []
  var arrs = val.result.errorMsg.split('\n')
  var h = obj.$createElement
  for (var i in arrs) {
    msgs.push(h('p', null, arrs[i]))
  }
  obj.$confirm('提示', {
    title: '提示',
    message: h('div', null, msgs),
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
}

// 保存触发
export function save(obj, close) {
  if (obj.buyerId === null || obj.buyerId === '') {
    Message({
      message: '请选择采购!',
      type: 'warning',
      duration: 3 * 1000
    })
    return
  }
  obj.$confirm('是否保存该item信息?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    obj.loading = true
    obj.itemInfo.filePath = obj.$refs.mpsFileForm.itemInfo.filePath
    obj.itemInfo.priceOBFormInfo = obj.$refs.priceForm.priceOBFormInfo
    const data = { id: obj.id, itemNbr: obj.itemInfo.itemNbr, buyerId: obj.buyerId, changeType: obj.parent.changeType, itemInfo: JSON.stringify(obj.itemInfo) }
    addOrUpdate(data, obj.parent.addOrUpdate).then((res) => {
      obj.parent.init()
      showMessage(obj, res)
      obj.itemStatus = res.result.itemStatus
      obj.loading = false
      if (close) {
        obj.dialog = false
      }
    }, error => {
      console.log(error)
      obj.loading = false
    })
  })
}

// 初始化加载
export function initPanel(obj) {
  obj.$refs.inputSheetForm.itemInfo = obj.itemInfo
  obj.$refs.ftagForm.ftagFormInfo = obj.itemInfo.ftagFormInfo ? obj.itemInfo.ftagFormInfo : {}
  obj.$refs.priceForm.priceOBFormInfo = obj.itemInfo.priceOBFormInfo ? obj.itemInfo.priceOBFormInfo : []
  obj.$refs.igroForm.igroFormInfo = obj.itemInfo.igroFormInfo ? obj.itemInfo.igroFormInfo : {}
  obj.$refs.cpkAndUnitForm.cpkAndUnitInfo = obj.itemInfo.cpkAndUnitInfo ? obj.itemInfo.cpkAndUnitInfo : {}
  obj.$refs.pbItemForm.pbItemInfo = obj.itemInfo.pbItemInfo ? obj.itemInfo.pbItemInfo : {}
  obj.$refs.mpsFileForm.itemInfo = obj.itemInfo ? obj.itemInfo : {}
  obj.$refs.mpsFileForm.filePath = obj.itemInfo ? obj.itemInfo.filePath : null
  obj.$refs.mpsFileForm.vendorItemInfo = obj.itemInfo.vendorItemInfo ? obj.itemInfo.vendorItemInfo : {}
}

// 批量校验
export function doBatchValidate(obj) {
  if (obj.ids === undefined || obj.ids === null || obj.ids.length === 0) {
    Message({
      message: '请选择需要校验的item。',
      type: 'error',
      duration: 3 * 1000
    })
    return
  }
  obj.$confirm('确认校验选择的 ' + obj.ids.length + ' 条item信息?', '校验数据', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    obj.loading = true
    batchValidate({ ids: obj.ids, changeType: obj.changeType }).then(res => {
      obj.toQuery()
      obj.$alert('校验处理中，请稍后刷新页面进行查询。', '提交成功', {
        confirmButtonText: '确定',
        type: 'success'
      })
      obj.loading = false
    }, error => {
      console.log(error)
      obj.loading = false
    })
  })
}
